package com.ebrothers.isns.common;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.ebrothers.isns.vo.Location;

public final class Utils {

	private static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss";

	public static String dateToString(Date date) {
		SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_PATTERN);
		return format.format(date);
	}

	public static Date stringToDate(String date) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_PATTERN);
		return format.parse(date);
	}

	public static Location stringToLocation(String location) {
		Location loc = null;
		if (location != null && location.contains(":")) {
			String[] splits = location.split(":");
			if (splits.length == 2) {
				String name = splits[0];
				splits = splits[1].split(",");
				if (splits.length == 2) {
					loc = new Location();
					loc.setName(name);
					loc.setLongitude(splits[0]);
					loc.setLatitude(splits[1]);
				}
			}
		}
		return loc;
	}

	public static String locationToString(Location loc) {
		return loc.getLongitude() + ":" + loc.getLatitude();
	}

	public static String decodeText(String text) {
		if (text == null) {
			return null;
		}
		try {
			text = new String(text.getBytes("iso8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return text;
	}

	public static String encodeText(String text) {
		if (text == null) {
			return null;
		}
		// try {
		// text = URLEncoder.encode(text, "utf-8");
		// } catch (UnsupportedEncodingException e) {
		// }
		return text;
	}
}
